<?php

// 获取当前登录用户
if (! function_exists('auth_user')) {
    /**
     * Get the auth_user.
     *
     * @return mixed
     */
    function auth_user()
    {
        return app('Dingo\Api\Auth\Auth')->user();
    }
}

if (! function_exists('dingo_route')) {
    /**
     * 根据别名获得url.
     *
     * @param string $version
     * @param string $name
     * @param string $params
     *
     * @return string
     */
    function dingo_route($version, $name, $params = [])
    {
        return app('Dingo\Api\Routing\UrlGenerator')
            ->version($version)
            ->route($name, $params);
    }
}

if (! function_exists('trans')) {
    /**
     * Translate the given message.
     *
     * @param string $id
     * @param array  $parameters
     * @param string $domain
     * @param string $locale
     *
     * @return string
     */
    function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
    {
        if (is_null($id)) {
            return app('translator');
        }

        return app('translator')->trans($id, $parameters, $domain, $locale);
    }
}

if (! function_exists('kv_rows')) {
    /**
     * Translate the kv to rows.
     *
     * @param array  $rows
     *
     * @return array
     */
    function kv_rows($rows, $field)
    {
        $arr = array();
		if($rows){
			foreach($rows as $row){
				if(isset($row[$field])){
					$arr[$row[$field]] = $row;
				}   
			}   
		}   
		return $arr;
    }
}


if (! function_exists('kv_vals')) {
    /**
     * Translate the given message.
     *
     * @param array  $rows
     *
     * @return array
     */
    function kv_vals($rows, $field, $vField)
    {
        $arr = array();
		if($rows){
			$itemIdArr = [];
			foreach($rows as $row){
				if(isset($row[$field])){
					$arr[$row[$field]][] = $row[$vField];
				}
			}
		}
		return $arr;
    }
}

if (! function_exists('field_vals')) {
    /**
     * Translate the given message.
     *
     * @param array  $rows
     *
     * @return array
     */
    function field_vals($tmpRows, $field)
    {
        $arr = array();
		if($tmpRows && is_array($tmpRows)){
			foreach($tmpRows as $row){
				if(isset($row[$field])){
					$arr[] = $row[$field];
				}
			}
		}
		return $arr;
    }
}

if (! function_exists('object_to_array')) {
	function object_to_array($obj)
	{
		$_arr = is_object($obj) ? get_object_vars($obj):$obj;
		$arr = null;
		foreach($_arr as $key=>$val){
			$val = (is_array($val)) || is_object($val) ? object_to_array($val):$val;
			$arr[$key]=$val;
		}
		return $arr;
	}
}